package org.xncj.Controller;

import org.constant.MessageConstant;
import org.entity.PageResult;
import org.entity.QueryPageBean;
import org.entity.Result;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.web.bind.annotation.*;
import org.xncj.CheckItem;
import org.xncj.CheckItemSerivce;


import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/checkItem")
public class CheckItemController {
    @Resource
    public CheckItemSerivce checkItemSerivce;
    @Autowired
    private HttpMessageConverters messageConverters;

    @RequestMapping("/addcheckitem")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        try {
            checkItemSerivce.addCheckItem(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkItemSerivce.findPage(queryPageBean);
        return pageResult;
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable ("id")Integer checkItemId){
        try {
            checkItemSerivce.deleteById(checkItemId);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);

        }
    }
    @PutMapping("/updateCheckItem")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){
        try {
            checkItemSerivce.updateCheckItem(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckItem> checkItems =  null;
        try {
            checkItems = checkItemSerivce.findAll();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItems);
    }
}
